package com.hex.ds.hdrs.period.task.controller;


import com.hex.bigdata.hbda.annotation.AnonymousAccess;
import com.hex.bigdata.hbda.annotation.Log;
import com.hex.bigdata.hbda.base.Page;
import com.hex.ds.hdrs.common.page.PageData;
import com.hex.ds.hdrs.period.job.service.PeriodJobDefService;
import com.hex.ds.hdrs.period.task.converter.IPeriodTaskDefConverter;
import com.hex.ds.hdrs.period.task.converter.IPeriodTaskDefVoConverter;
import com.hex.ds.hdrs.period.task.dto.PeriodTaskDefDto;
import com.hex.ds.hdrs.period.task.po.PeriodTaskDef;
import com.hex.ds.hdrs.period.task.service.PeriodTaskDefService;
import com.hex.ds.hdrs.period.task.vo.PeriodTaskDefVo;
import com.hex.ds.hdrs.period.task.vo.TaskDependDetailVO;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;

import javax.validation.constraints.NotBlank;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;

/**
 * @Package: com.hex.ds.hdrs.period.task.controller
 * @ClassName PeriodTaskDefController
 * @Description: 周期任务定义管理
 * @Author: wz.li
 * @Date 2023/3/30 11:24
 * @Version v2.0.2
 **/
@Slf4j
@RestController
@RequestMapping("/api/periodTaskDef")
public class PeriodTaskDefController {

    @Autowired
    private PeriodTaskDefService periodTaskDefService;

    @Autowired
    private IPeriodTaskDefVoConverter converter;

    @Autowired
    private PeriodJobDefService periodJobDefService;

    @Log("判断任务编码是否重复")
    @ApiOperation("判断任务编码是否重复")
    @PostMapping(value = "/validateTaskCode")
    public ResponseEntity<Object> validateTaskCode(@RequestBody Map paramMap) {
        log.debug("判断任务编码是否重复【PeriodTaskDefController.validateTaskCode】参数paramMap:  {}", paramMap.toString());
        return new ResponseEntity<>(this.periodTaskDefService.validateTaskCode(paramMap), HttpStatus.OK);
    }

    /*
     * @Method: add <br>
     * @Param: [periodTaskDefVo] <br>
     * @Return: org.springframework.http.ResponseEntity<java.lang.Object> <br>
     * @Description：新增周期任务定义<br>
     * @Author： wz.li<br>
     * @Date： 2023/3/30 11:51 <br>
     * @Version： V2.0.2<br>
     */
    @PostMapping
    @Log("新增周期任务定义")
    @ApiOperation("新增周期任务定义")
//    @AnonymousAccess
    public ResponseEntity<Object> add(@RequestBody PeriodTaskDefVo periodTaskDefVo) {
        log.info("【PeriodTaskDefController.add】新增周期任务定义，参数: {}", periodTaskDefVo.toString());
        periodTaskDefService.add(converter.toPeriodTaskDefDto(periodTaskDefVo));
        return new ResponseEntity<>(true, HttpStatus.OK);
    }

    @Log("根据作业编号查询任务列表")
    @ApiOperation("根据作业编号查询任务列表")
    @GetMapping(value = "/queryTaskList/{jobCode:.+}")
    public ResponseEntity<Object> queryTaskList(@PathVariable("jobCode") String jobCode) {
        log.debug("根据作业编号查询任务列表【PeriodTaskDefController.queryTaskList】参数jobCode: {}", jobCode);
        List<PeriodTaskDef> periodTaskDefList = periodJobDefService.qryTaskDefVoList(jobCode);
        return ResponseEntity.ok(periodTaskDefList);
    }

    /*
     * @Method: propToUpadate <br>
     * @Param: [periodTaskDefVo] <br>
     * @Return: org.springframework.http.ResponseEntity<java.lang.Object> <br>
     * @Description：更新周期任务定义<br>
     * @Author： wz.li<br>
     * @Date： 2023/3/30 11:54 <br>
     * @Version： V2.0.2<br>
     */
    @PutMapping
    @Log("更新周期任务定义")
    @ApiOperation("更新周期任务定义")
    public ResponseEntity<Object> update(@RequestBody PeriodTaskDefVo periodTaskDefVo) {
        log.info("【PeriodTaskDefController.update】更新周期任务定义，参数: {}", periodTaskDefVo.toString());
        periodTaskDefService.modifyById(converter.toPeriodTaskDefDto(periodTaskDefVo));
        return new ResponseEntity<>(true, HttpStatus.OK);
    }

    @ApiOperation("批量删除")
    @PostMapping("/doDeleteByPkId")
    public ResponseEntity<Object> doDeleteByPkId(@RequestBody List<PeriodTaskDef> list) {
        log.debug("初始化作业和任务信息【PeriodJobDefController.doDeleteByPkId】参数list: {}", list.toArray());
        return new ResponseEntity<>(periodTaskDefService.doDeleteByPkId(list), HttpStatus.OK);
    }

    @Log("获取当前应用所有依赖任务")
    @ApiOperation("获取当前应用所有依赖任务")
    @PostMapping("/getDependTaskByCurrentApp")
    public ResponseEntity<Object> getDependTaskByCurrentApp(@RequestBody Map map) {
        log.debug("获取依赖任务信息【PeriodJobDefController.getDependTaskByCurrentApp】");
        PageData pageData = this.periodTaskDefService.getDependTaskByCurrentApp(map);
        return new ResponseEntity<>(pageData, HttpStatus.OK);
    }

    @Log("获取当前应用所有任务")
    @ApiOperation("获取当前应用所有任务")
    @GetMapping("/getTaskListByCurrentApp")
    public ResponseEntity<Object> getTaskListByCurrentApp(@RequestParam @NotBlank String appCode) {
        log.debug("获取任务信息【PeriodJobDefController.getTaskListByCurrentApp】");
        return new ResponseEntity<>(this.periodTaskDefService.getTaskListByCurrentApp(appCode), HttpStatus.OK);
    }

    @ApiOperation("通过name与code获取当前应用所有任务")
    @GetMapping("/getTaskList")
    public ResponseEntity<Object> getTaskList(TaskDependDetailVO taskDependDetail, Page page) {
        log.debug("获取依赖图信息【PeriodJobDefController.getTaskListByCurrentApp】");
        PageData data = new PageData(this.periodTaskDefService.getTaskList(taskDependDetail, page), page);
        return new ResponseEntity<>(data, HttpStatus.OK);
    }

    @ApiOperation("批量更新任务优先级")
    @PostMapping("/batchUpdateSortNo")
    public ResponseEntity<Object> batchUpdatePriorityLevel(@RequestBody PeriodTaskDefDto periodTaskDefDto) {
        log.debug("批量更新任务优先级【PeriodJobDefController.batchUpdateOffsetHour】参数periodJobDefDto: {}", periodTaskDefDto.toString());
        this.periodTaskDefService.batchUpdatePriorityLevel(periodTaskDefDto);
        return new ResponseEntity<>(true, HttpStatus.OK);
    }

    @Log("批量修改任务信息")
    @ApiOperation("批量修改任务信息")
    @PostMapping("/batchUpdateTaskInf")
    public ResponseEntity<Object> batchUpdateTaskInf(@RequestBody PeriodTaskDefDto periodTaskDefDto) {
        log.debug("批量修改任务信息【PeriodJobDefController.batchUpdateTaskInf】参数periodTaskDefDto: {}", periodTaskDefDto.toString());
        this.periodTaskDefService.batchUpdateTaskInf(periodTaskDefDto);
        return new ResponseEntity<>(true, HttpStatus.OK);
    }

    @Log("批量修改任务配置根据jobCode")
    @ApiOperation("批量修改任务配置根据jobCode")
    @PostMapping("/batchUpdateTaskByJobCode")
    public ResponseEntity<Object> batchUpdateTaskByJobCode(@RequestBody PeriodTaskDefDto periodTaskDefDto) {
        log.debug("批量修改任务配置根据jobCode【PeriodJobDefController.batchUpdateTaskByJobCode】参数:  {}", periodTaskDefDto.toString());
        this.periodTaskDefService.batchUpdateTaskByJobCode(periodTaskDefDto);
        return new ResponseEntity<>(true, HttpStatus.OK);
    }

    @GetMapping("/selectPage")
    @ApiOperation("分页查询")
    public ResponseEntity<Object> selectPage(PeriodTaskDefDto periodTaskDefDto, Page page) {
        log.info("【PeriodJobDefController.selectPage】periodTaskDefDto:  {}", periodTaskDefDto);
        PageData data = new PageData(this.periodTaskDefService.selectPage(periodTaskDefDto, page), page);
        return new ResponseEntity<>(data, HttpStatus.OK);
    }

    @GetMapping("/findMutexTaskList")
    @ApiOperation("互斥任务列表")
    @Log("互斥列表查询")
    public ResponseEntity<Object> findMutexTaskList(PeriodTaskDefDto periodTaskDefDto, Page page) {
        log.info("【PeriodJobDefController.findMutexTaskList】periodTaskDefDto:  {}", periodTaskDefDto);
        PageData data = new PageData(this.periodTaskDefService.findMutexTaskList(periodTaskDefDto, page), page);
        return new ResponseEntity<>(data, HttpStatus.OK);
    }

    /*
     * @Method: upDependChat <br>
     * @Param: [taskCode] <br>
     * @Return: org.springframework.http.ResponseEntity<java.lang.Object> <br>
     * @Description：获取指定周期任务定义上游依赖图<br>
     * @Author： wz.li<br>
     * @Date： 2023/4/24 17:01 <br>
     * @Version： V2.0.2<br>
     */
    @Log("周期任务定义上游依赖图")
    @ApiOperation("周期任务定义上游依赖图")
    @GetMapping("/upDependChat/{taskCode:.+}/{showType}")
    public ResponseEntity<Object> upDependChat(@PathVariable("taskCode") String taskCode,
                                               @PathVariable("showType") String showType) {
        log.debug("【PeriodTaskDefController.upDependChat】上游依赖图: {}", taskCode);
        return ResponseEntity.ok(periodTaskDefService.upDependChat(taskCode, showType));
    }
    @Log("周期任务定义上游依赖")
    @ApiOperation("周期任务定义上游依赖")
    @GetMapping("/upDependChat/{taskCode:.+}")
    public ResponseEntity<Object> upDepend(@PathVariable("taskCode") String taskCode
                                              ) {
        log.debug("【PeriodTaskDefController.upDependChat】上游依赖图: {}", taskCode);
        return ResponseEntity.ok(periodTaskDefService.upDepend(taskCode));
    }


    @Log("周期任务定义查询单个任务定义")
    @ApiOperation("周期任务定义查询单个任务定义")
    @GetMapping("/taskDef/{taskCode:.+}")
    public ResponseEntity<Object> taskDef(@PathVariable("taskCode") String taskCode
    ) {
        log.debug("【PeriodTaskDefController.upDependChat】周期任务定义查询单个任务定义: {}", taskCode);
        return ResponseEntity.ok(periodTaskDefService.taskDef(taskCode));
    }

    /*
     * @Method: upDependList <br>
     * @Param: [taskCode] <br>
     * @Return: org.springframework.http.ResponseEntity<java.lang.Object> <br>
     * @Description：周期任务定义上游依赖列表<br>
     * @Author： wz.li<br>
     * @Date： 2023/4/24 17:02 <br>
     * @Version： V2.0.2<br>
     */
    @Log("周期任务定义上游依赖列表")
    @ApiOperation("周期任务定义上游依赖列表")
    @GetMapping("/upDependList/{taskCode:.+}")
    public ResponseEntity<Object> upDependList(@PathVariable("taskCode") String taskCode) {
        log.debug("【PeriodTaskDefController.upDependList】上游依赖列表: {}", taskCode);
        return ResponseEntity.ok(periodTaskDefService.upDependList(taskCode));
    }

    @Log("周期任务定义下游依赖")
    @ApiOperation("周期任务定义下游依赖")
    @GetMapping("/downDependChat/{taskCode:.+}")
    public ResponseEntity<Object> downDepend(@PathVariable("taskCode") String taskCode) {
        log.debug("【PeriodTaskDefController.downDepend】下游依赖: {}", taskCode);
        return ResponseEntity.ok(periodTaskDefService.downDepend(taskCode));
    }

    /*
     * @Method: downDependChat <br>
     * @Param: [taskCode] <br>
     * @Return: org.springframework.http.ResponseEntity<java.lang.Object> <br>
     * @Description：周期任务定义下游依赖图<br>
     * @Author： wz.li<br>
     * @Date： 2023/4/24 17:02 <br>
     * @Version： V2.0.2<br>
     */
    @Log("周期任务定义下游依赖图")
    @ApiOperation("周期任务定义下游依赖图")
    @GetMapping("/downDependChat/{taskCode:.+}/{showType}")
    public ResponseEntity<Object> downDependChat(@PathVariable("taskCode") String taskCode,
                                                 @PathVariable("showType") String showType) {
        log.debug("【PeriodTaskDefController.downDependChat】下游依赖图: {}", taskCode);
        return ResponseEntity.ok(periodTaskDefService.downDependChat(taskCode, showType));
    }

    /*
     * @Method: downDependList <br>
     * @Param: [taskCode] <br>
     * @Return: org.springframework.http.ResponseEntity<java.lang.Object> <br>
     * @Description：周期任务定义下游依赖列表<br>
     * @Author： wz.li<br>
     * @Date： 2023/4/24 17:02 <br>
     * @Version： V2.0.2<br>
     */
    @Log("周期任务定义下游依赖列表")
    @ApiOperation("周期任务定义下游依赖列表")
    @GetMapping("/downDependList/{taskCode:.+}")
    public ResponseEntity<Object> downDependList(@PathVariable("taskCode") String taskCode) {
        log.debug("【PeriodTaskDefController.downDependList】下游依赖列表: {}", taskCode);
        return ResponseEntity.ok(periodTaskDefService.downDependList(taskCode));
    }
}

